package org.example.springmvc_demo6.controller;

import org.example.springmvc_demo6.model.MessageInfo;
import org.example.springmvc_demo6.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/message")
@RestController
public class MessageController {

    @Autowired
    public MessageService messageService;

    @RequestMapping("/publish")
    public Boolean publish(MessageInfo messageInfo){
        System.out.println("接收参数, messageInfo:"+messageInfo);

        if(!StringUtils.hasLength(messageInfo.getFrom()) || !StringUtils.hasLength(messageInfo.getTo())
                ||!StringUtils.hasLength(messageInfo.getMessage())){
            return false;
        }

        Integer result = messageService.publicMessage(messageInfo);

        return result > 0;

    }

    @RequestMapping("/getMessage")
    public List<MessageInfo> getMessage(){
        return messageService.getMessage();
    }


}
